﻿// See https://aka.ms/new-console-template for more information
using MySql.Data.MySqlClient;
using Renci.SshNet;

var privateKeyFile = new PrivateKeyFile(@"E:\cn-dev-sanoflow.pem", "25flow#SinoFlow$DDM2979");
var keyFiles = new[] { privateKeyFile };

var connectInfo = new ConnectionInfo("47.96.254.88", 7253, "sanoflow",new PrivateKeyAuthenticationMethod("sanoflow", keyFiles));

using (var client = new SshClient(connectInfo))
{
    client.Connect();
    Console.WriteLine("SshClient Connected!");
    var portForwarded = new ForwardedPortLocal("127.0.0.1", 3308, "10.40.1.85", 3306);
    client.AddForwardedPort(portForwarded);
    portForwarded.Start();
    Console.WriteLine("ForwardedPortLocal Started!");
    string connectionString = "server=127.0.0.1;port=3308;database=eoclm;uid=ddm;password=25flow#SinoFlow$DDM2979;";
    MySqlConnection connection = new MySqlConnection(connectionString);

    try
    {
        connection.Open();
        Console.WriteLine("MySql Connected!");

        // 在这里可以执行数据库操作

        string query = "SELECT * FROM USERS LIMIT 3";
        MySqlCommand command = new MySqlCommand(query, connection);
        MySqlDataReader reader = command.ExecuteReader();
        Console.WriteLine("SELECT DATA SUCCESS!");
        Console.WriteLine("SELECT DATA Output：");
        while (reader.Read())
        {
            Console.WriteLine($"column1: {reader[0].ToString()}, column2: {reader[1].ToString()}");
        }

        reader.Close();
        connection.Close();
    }
    catch (MySqlException ex)
    {
        Console.WriteLine("连接到数据库时出错：" + ex.Message);
    }

    portForwarded.Stop();
    client.Disconnect();
}